home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / ulog / Ulog_GetAllLogins.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-13  |  3.1 KB  |  104 lines

  1. /* 
  2.  * Ulog_GetAllLogins.c --
  3.  *
  4.  *    Source code for the Ulog_GetAllLogins procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/ulog/RCS/Ulog_GetAllLogins.c,v 1.4 89/01/13 11:49:34 douglis Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20.  
  21. #include <ulog.h>
  22. #include "ulogInt.h"
  23.  
  24.  
  25. /*
  26.  *----------------------------------------------------------------------
  27.  *
  28.  * Ulog_GetAllLogins --
  29.  *
  30.  *    Get the records for all valid login records, up to the number of
  31.  *     records specified.  If the records correspond to a machine
  32.  *    that is down, they will still be returned and it is the responsibility
  33.  *     of the caller to verify the status of each machine.
  34.  *
  35.  * Results:
  36.  *    -1 indicates an error, in which case errno indicates more details.
  37.  *    On success, the number of structures being returned in *dataPtr
  38.  *    is returned as the value of the function.
  39.  *    *locPtr is set to the next location to be searched.
  40.  *
  41.  * Side effects:
  42.  *    The database file is opened for reading, locked, and later closed.
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46.  
  47. #define NUMBUF 100
  48.  
  49. int
  50. Ulog_GetAllLogins(numEntries, locPtr, dataPtr)
  51.     int numEntries;        /* number of structures in *dataPtr */
  52.     int *locPtr;        /* index of first structure to return, set
  53.                  * to first unseen structure on return */
  54.     Ulog_Data *dataPtr;        /* Pointer to array of structures */
  55. {
  56.     int status;
  57.     Db_Handle handle;
  58.     int i;
  59.     register int loc = *locPtr;
  60.     char buffer[ULOG_RECORD_LENGTH];
  61.     Ulog_Data *thisPtr;
  62.     int count;
  63.  
  64.     status = Db_Open(ULOG_FILE_NAME, ULOG_RECORD_LENGTH, &handle, 0,
  65.              DB_LOCK_OPEN, DB_LOCK_BREAK, NUMBUF);
  66.     if (status != 0) {
  67.     return(status);
  68.     }
  69.     i = 0;
  70.     while(i < numEntries) {
  71.     status = Db_Get(&handle, buffer, loc);
  72.     thisPtr = &dataPtr[i];
  73.     /*
  74.      * We go until we get a failure status, then return what we
  75.      * have so far.
  76.      */
  77.     if (status == -1) {
  78.         break;
  79.     }
  80.     count = sscanf(buffer, ULOG_FORMAT_STRING, &thisPtr->uid,
  81.                &thisPtr->hostID, &thisPtr->portID,
  82.                &thisPtr->updated, thisPtr->location);
  83.     /*
  84.      * Only return a record if its updated field is non-zero (i.e.,
  85.      * never initialized or invalidated by being reset to 0.
  86.      * In this case, increment i so the next record goes in the
  87.      * next location, and increment the record number to get.  If
  88.      * sscanf can't parse the record properly, assume it's invalid.
  89.      * It's okay if the location field doesn't match because it
  90.      * may be empty. 
  91.      */
  92.     if (thisPtr->updated != 0 && count >= ULOG_ITEM_COUNT - 1) {
  93.         if (count == ULOG_ITEM_COUNT - 1) {
  94.         thisPtr->location[0] = '\0';
  95.         }
  96.         i++;
  97.     }
  98.     loc++;
  99.     }
  100.     (void) Db_Close(&handle);
  101.     *locPtr = loc;
  102.     return(i);
  103. }
  104.